import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * 
 * @author alanazwale
 *
 */
public class GUI extends JFrame implements java.awt.event.ActionListener{

	JTextArea output;
	JScrollPane scrollPane;

	public JMenuBar createMenuBar() {
		JMenuBar menuBar;



		//Create the menu bar.
		//Create the menu bar.


		menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		//Build the first menu.
		JMenu menu = new JMenu("Menu");
		JMenu edit = new JMenu("edit");
		//add to the menu bar
		menuBar.add(menu);
		menuBar.add(edit);
		//add Items to the Menu
		JMenuItem newAction = new JMenuItem("New");
		JMenuItem openAction = new JMenuItem("Open");
		JMenuItem exitAction = new JMenuItem("Exit");
		// add to the menu
		menu.add(newAction);
		menu.add(openAction);
		menu.add(exitAction);

		JMenuItem help = new JMenuItem("Help");
		edit.add(help);


		/** Listen to the new command in menu 
		 * 
		 */
		newAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				JFileChooser chooser = new JFileChooser();
				int returnVal = chooser.showOpenDialog(chooser);
				if(returnVal == JFileChooser.APPROVE_OPTION) {
					System.out.println("You chose to open this file: " + 
							chooser.getSelectedFile().getName());     // get the file name that you need to parse.
				}
				String name = chooser.getSelectedFile().getName();

				try {

					//Runtime.exec("java -jar trang.jar 5-something.xml 5.1-somethingElse.xsd");
					System.out.println("Calling jar");

					Process p = Runtime.getRuntime().exec("java -jar /u/students/alanazwale/Desktop/smalibaksmali/baksmali-1.3.2.jar -o"+" opo "+name);
					System.out.println("java -jar /u/students/alanazwale/Desktop/smalibaksmali/baksmali-1.3.2.jar -o"+" opo "+name);

					BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
					synchronized (p) {
						try {
							p.waitFor();
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					System.out.println(p.exitValue());
					int b=0;
					while((b=bis.read()) >0){

						System.out.print((char)b);    
					}        
					System.out.println("");
					System.out.println("Called jar");



				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 


			}
		});
		/** Listen to the open command in menu 
		 * 
		 */
		openAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				JFileChooser chooser = new JFileChooser();
				int returnVal = chooser.showOpenDialog(chooser);
				if(returnVal == JFileChooser.APPROVE_OPTION) {
					System.out.println("You chose to open this file: " + 
							chooser.getSelectedFile().getName());     // get the file name that you need to parse.
				}
				String name = chooser.getSelectedFile().getName();

				try {

					//Runtime.exec("java -jar trang.jar 5-something.xml 5.1-somethingElse.xsd");
					System.out.println("Calling jar");
					Process p = Runtime.getRuntime().exec("java -jar apktool.jar d"+" "+name);
					BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
					synchronized (p) {
						try {
							p.waitFor();
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					System.out.println(p.exitValue());
					int b=0;
					while((b=bis.read()) >0){

						System.out.print((char)b);    
					}        
					System.out.println("");
					System.out.println("Called jar");

				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 

			}
		});
		/** Listen to the exit command in menu, and exit the game.
		 * 
		 */
		exitAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//System.out.println(((AbstractButton) e.getSource()).getActionCommand());
				System.exit(1);
			}
		});
		return menuBar;

	}

	public Container createContentPane() {
		//Create the content-pane-to-be.
		Container content = getContentPane();
		content.setBackground(Color.lightGray);
		
		
		
		JPanel contentPane = new JPanel(new GridLayout(2, 3));
		
		JPanel buttons = new JPanel(new GridLayout(3, 3));
		buttons.setBorder(BorderFactory.createTitledBorder("Commands:"));
		
		
		contentPane.setBorder(BorderFactory.createTitledBorder("Information:"));
		contentPane.setOpaque(true);

		JButton south  = new JButton("Parse");
		JButton south2  = new JButton("Adding a Method");
		JButton add  = new JButton("Generate final copy!");
		//Create a scrolled text area.
		output = new JTextArea(5, 30);
		output.setEditable(false);
		//scrollPane = new JScrollPane(output);

		
		
//		b2.setVerticalTextPosition(AbstractButton.BOTTOM);
//		b2.setHorizontalTextPosition(AbstractButton.CENTER);
//		south.setMnemonic(KeyEvent.VK_M);
//
		south.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//System.out.println(((AbstractButton) e.getSource()).getActionCommand());
				try {
					parsing.chooseFile();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		south2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//System.out.println(((AbstractButton) e.getSource()).getActionCommand());
				try {
					InstCode.chooseFile();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//System.out.println(((AbstractButton) e.getSource()).getActionCommand());
				try {
					//add the method addCallMethod and read user input
					parsing.chooseFile2();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		//Add the text area to the content pane.
		

		buttons.add(south);
		buttons.add(south2);
		buttons.add(add);
		
		contentPane.add(buttons);
		
		content.add(buttons, BorderLayout.EAST);
		
		
		
		return content;
	}

	/** Returns an ImageIcon, or null if the path was invalid. */
	protected static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = GUI.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	/**
	 * Create the GUI and show it.  For thread safety,
	 * this method should be invoked from the
	 * event-dispatching thread.
	 */
	private static void createAndShowGUI() {
		//Create and set up the window.
		JFrame frame = new JFrame("MenuLookDemo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Create and set up the content pane.
		GUI demo = new GUI();
		frame.setJMenuBar(demo.createMenuBar());
		frame.setContentPane(demo.createContentPane());

		//Display the window.
		frame.setSize(600, 750);

		frame.setVisible(true);
	}

	public static void main(String[] args) {
		//Schedule a job for the event-dispatching thread:
		//creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}


}
